﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using YArchitech.LIB;
using HYCodeBase.XmlUtility;

namespace HYRevitCode.FormUtility
{
	public class SelectFloorUtility
	{
		public static bool ReadSelectFloorsForm(string strCommandName, string strRevitFile, CheckedListBox chkSelectFloors)
		{
			string value = strRevitFile.Split(new char[]
			{
				'\\'
			}).Last<string>().ToString();
			string dataLocation = Product.DataLocation;
			string strUseFrontPath = SelectFloorUtility.m_strUseFrontPath;
			string text = Path.Combine(dataLocation, strUseFrontPath, SelectFloorUtility.m_strBaseXmlFileName);
			string sourceFileName = Path.Combine(dataLocation, SelectFloorUtility.m_strBaseXmlFileName);
			try
			{
				if (!File.Exists(text))
				{
					File.Copy(sourceFileName, text);
				}
				HYCodeBase.XmlUtility.XmlHelper xmlHelper = new HYCodeBase.XmlUtility.XmlHelper(text, SelectFloorUtility.m_strNodeNameOfRoot);
				XmlElement xmlRootNode = xmlHelper.XmlRootNode;
				List<XmlElement> elements = xmlHelper.GetElements();
				for (int i = 0; i < elements.Count; i++)
				{
					string attribute = elements[i].GetAttribute(SelectFloorUtility.m_strNodeNameOfCmd);
					string attribute2 = elements[i].GetAttribute(SelectFloorUtility.m_strNodeNameOfRevitPath);
					if (attribute.Equals(strCommandName) && attribute2.Equals(value))
					{
						List<XmlElement> list = new List<XmlElement>();
						foreach (object obj in elements[i].ChildNodes)
						{
							XmlElement item = obj as XmlElement;
							list.Add(item);
						}
						for (int j = 0; j < list.Count; j++)
						{
							string attribute3 = list[j].GetAttribute(SelectFloorUtility.m_strNameAttribute);
							for (int k = 0; k < chkSelectFloors.Items.Count; k++)
							{
								if (attribute3.Equals(chkSelectFloors.Items[k].ToString()))
								{
									chkSelectFloors.SetItemChecked(k, true);
								}
							}
						}
					}
				}
				if (chkSelectFloors.CheckedItems.Count < 1)
				{
					for (int l = 0; l < chkSelectFloors.Items.Count; l++)
					{
						chkSelectFloors.SetItemChecked(l, true);
					}
				}
			}
			catch (Exception)
			{
				return false;
			}
			return true;
		}

		public static bool WriteSelectFloorsToFile(string strCommandName, string strRevitFile, CheckedListBox chkSelectFloors)
		{
			string text = strRevitFile.Split(new char[]
			{
				'\\'
			}).Last<string>().ToString();
			string dataLocation = Product.DataLocation;
			string strUseFrontPath = SelectFloorUtility.m_strUseFrontPath;
			string text2 = Path.Combine(dataLocation, strUseFrontPath, SelectFloorUtility.m_strBaseXmlFileName);
			string sourceFileName = Path.Combine(dataLocation, SelectFloorUtility.m_strBaseXmlFileName);
			try
			{
				if (!File.Exists(text2))
				{
					File.Copy(sourceFileName, text2);
				}
				HYCodeBase.XmlUtility.XmlHelper xmlHelper = new HYCodeBase.XmlUtility.XmlHelper(text2, SelectFloorUtility.m_strNodeNameOfRoot);
				XmlElement xmlRootNode = xmlHelper.XmlRootNode;
				XmlElement xmlCurNode = xmlHelper.XmlCurNode;
				XmlDocument xmlDoc = xmlHelper.XmlDoc;
				foreach (XmlElement xmlElement in xmlHelper.GetElements())
				{
					string attribute = xmlElement.GetAttribute(SelectFloorUtility.m_strNodeNameOfCmd);
					string attribute2 = xmlElement.GetAttribute(SelectFloorUtility.m_strNodeNameOfRevitPath);
					if (strCommandName.Equals(attribute) && text.Equals(attribute2))
					{
						xmlRootNode.RemoveChild(xmlElement);
					}
				}
				XmlElement xmlElement2 = xmlDoc.CreateElement(SelectFloorUtility.m_strNameRviteNode);
				SelectFloorUtility.AddNodeAttibute(xmlDoc, xmlRootNode, xmlElement2, SelectFloorUtility.m_strNodeNameOfCmd, strCommandName);
				SelectFloorUtility.AddNodeAttibute(xmlDoc, xmlRootNode, xmlElement2, SelectFloorUtility.m_strNodeNameOfRevitPath, text);
				for (int i = 0; i < chkSelectFloors.Items.Count; i++)
				{
					if (chkSelectFloors.GetItemChecked(i))
					{
						XmlElement curNode = xmlDoc.CreateElement(SelectFloorUtility.m_strNameRevitFloorNode);
						SelectFloorUtility.AddNodeAttibute(xmlDoc, xmlElement2, curNode, SelectFloorUtility.m_strNameAttribute, chkSelectFloors.Items[i].ToString());
					}
				}
				xmlHelper.SaveXml(text2);
			}
			catch (Exception)
			{
				return false;
			}
			return true;
		}

		private static void AddNodeAttibute(XmlDocument xmlDoc, XmlElement fatherNode, XmlElement curNode, string strAttributeName, string strAttribute)
		{
			XmlAttribute xmlAttribute = xmlDoc.CreateAttribute(strAttributeName);
			xmlAttribute.InnerText = strAttribute;
			curNode.Attributes.Append(xmlAttribute);
			fatherNode.AppendChild(curNode);
		}

		private static string m_strUseFrontPath = Product.UserDataLocation;

		private static string m_strBaseXmlFileName = "SelectFloors.xml";

		private static string m_strNodeNameOfRoot = "Root";

		private static string m_strNodeNameOfCmd = "CommandName";

		private static string m_strNodeNameOfRevitPath = "RvtPathName";

		private static string m_strNameAttribute = "Name";

		private static string m_strNameRviteNode = "RvtName";

		private static string m_strNameRevitFloorNode = "FloorName";
	}
}
